﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lenovo.CFI.BLL.DicBll;
using Lenovo.CFI.Common;
using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.BLL;


namespace Lenovo.CFI.Web.VP
{
    public partial class Home : System.Web.UI.UserControl
    {
        TopicBl topicBl = new TopicBl();
        IdeaBl ideaBl = new IdeaBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Topic> topicsAll = topicBl.GetTopic();

                List<Topic> topicsDefault = new List<Topic>();
                List<Topic> topicsMore = new List<Topic>();

                foreach (Topic t in topicsAll)
                {
                    if (t.Status == TopicStatus.Closed)
                        topicsMore.Add(t);
                    else
                        topicsDefault.Add(t);
                }


                this.RepTopic.DataSource = topicsDefault;
                this.RepTopic.DataBind();


                this.RepTopicMore.DataSource = topicsMore;
                this.RepTopicMore.DataBind();


                Dictionary<int, List<string>> topicReviewers = null;
                List<int> reviewTopicIDs = topicBl.GetReviewTopic(UserHelper.UserName, out topicReviewers);


                string tdcTopics = "";
                string reviewTopics = "";
                foreach (Topic topic in topicsAll)
                {
                    if (topic.Status == TopicStatus.Create && topic.Tdc == UserHelper.UserName)
                    {
                        tdcTopics += String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>&nbsp;&nbsp;",
                            UrlHelper.GenerateUrl("topictdc", "id", topic.ID.ToString()), topic.No);
                    }

                    if ((topic.Status == TopicStatus.Create || topic.Status == TopicStatus.Start ||
                        topic.Status == TopicStatus.Schedule || topic.Status == TopicStatus.Reviewer ||
                        topic.Status == TopicStatus.Review) && topicReviewers.ContainsKey(topic.ID))
                    {
                        if (topic.Status == TopicStatus.Create || topic.Status == TopicStatus.Start || topic.Status == TopicStatus.Schedule)
                            reviewTopics += (topic.No + "&nbsp;&nbsp;");
                        else if (topic.Status == TopicStatus.Reviewer)
                            reviewTopics += String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>&nbsp;&nbsp;",
                                UrlHelper.GenerateUrl("topicschedulelist", "id", topic.ID.ToString()), topic.No);
                        else
                        {
                            foreach (string reviewer in topicReviewers[topic.ID])   // 可能是多个
                            {
                                reviewTopics += String.Format(@"<a href=""{0}"" target=""_blank"">{1}{2}</a>&nbsp;&nbsp;",
                                    UrlHelper.GenerateUrl("topicideareview", "id", topic.ID.ToString(), "r", reviewer), topic.No,
                                    (reviewer != UserHelper.UserName) ? " [" +  reviewer + "]" : "");
                            }
                        }
                    }
                }
                this.LtrTDCTopics.Text = tdcTopics;
                this.LtrReviewerTopics.Text = reviewTopics;

                //HlMyIdeaCount.Text = ideaBl.GetMyIdeaListAll( UserHelper.UserName, null, null, null).Count.ToString();
                //IdeaTypeBl ideaTypeBl = new IdeaTypeBl();
                //List<IdeaType> types = ideaTypeBl.GetList(UserHelper.UserName);
                //string[] ideaTypes = new string[types.Count];
                //for (int i = 0; i < types.Count; i++)
                //{
                //    ideaTypes[i] = types[i].Code;
                //}
                //HlRevieaIdea.Text =
                //    ideaBl.GetIdeaList(-1, null, ideaTypes, null, IdeaStatus.Approving).FindAll(
                //        idea => idea.DcComment == null).Count.ToString();


                //RptTopicWork.DataSource = topicBl.GetTopicKickoffList();
                //RptTopicWork.DataBind();

                //RptTopicReview.DataSource = topicBl.GetTopicReviewList();
                //RptTopicReview.DataBind();

                //LbtnLess.Visible = false;

                //HlTeamIdea.NavigateUrl = string.Format("~/Default.aspx?vp=search&mgr={0}",UserHelper.UserName);
                //HlTeamTracking.NavigateUrl = string.Format("~/Default.aspx?vp=feedback&mgr={0}", UserHelper.UserName);
            }
        }

        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Literal LtrCollection = e.Row.FindControl("LtrCollection") as Literal;
                Literal LtrReview = e.Row.FindControl("LtrReview") as Literal;
                Literal LtrExecution = e.Row.FindControl("LtrExecution") as Literal;

                Topic topic = e.Row.DataItem as Topic;

                LtrCollection.Text = GetStatusText(topic, TopicStatus.Start);
                LtrReview.Text = GetStatusText(topic, TopicStatus.Review);
                LtrExecution.Text = GetStatusText(topic, TopicStatus.Execution);

            }
        }

        private string GetStatusText(Topic topic, TopicStatus status)
        {
            if (topic.Status == status)
                return "OnGoing";
            else if (topic.Status < status)
                return "&nbsp;";
            else
                return "Finished";
        }

        protected void RepTopic_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltrCollection = (Literal)e.Item.FindControl("LtrStatus1");
                Literal ltrReview = (Literal)e.Item.FindControl("LtrStatus2");
                Literal ltrExecution = (Literal)e.Item.FindControl("LtrStatus3");
                HyperLink hlTopic = (HyperLink)e.Item.FindControl("HlTopic");

                Topic topic = (Topic)e.Item.DataItem;

                ltrCollection.Text = GetStatusText(topic, TopicStatus.Start);
                ltrReview.Text = GetStatusText(topic, TopicStatus.Review);
                ltrExecution.Text = GetStatusText(topic, TopicStatus.Execution);
                hlTopic.Text = String.Format("Topic {0}: {1}", topic.No, topic.Title);
                hlTopic.NavigateUrl = UrlHelper.GenerateUrl("topicdetailgroup", "id", topic.ID.ToString());
            }
        }
    }
}